package com.khh.action;

import com.khh.dao.BookDao;
import com.khh.entity.Book;
import com.opensymphony.xwork2.ActionContext;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * Created by webrx on 2017-09-15.
 */
@Data
@Repository
public class BookAction {

    private int id;
    private String na;
    private double pr;
    @Autowired
    private BookDao dao;

    //测试
    public void abc(){
        System.out.println("hello-abc");
        //System.out.println(dao.query().size());
    }

    //查询
    public String queryAll(){
        ActionContext.getContext().put("book",dao.query());
        //System.out.println(dao.query().size());
        return "queryAll";
    }

    //添加数据
    public String insert(){

        Book book = new Book();
        book.setPrice(pr);
        book.setName(na);
        dao.insert(book);
        return "success";
    }

    //删除数据
    public String delete(){
        dao.deleteById(id);
        return "success";
    }

    //更新数据
    public String update(){
        Book book = new Book();
        book.setPrice(pr);
        book.setName(na);
        dao.update(book, id);
        return "success";
    }

    //查询通过id
    public String query(){
        ActionContext.getContext().put("book",dao.queryById(id));
        return "query";
    }

}
